package com.inool.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.inool.mapper.TeacherMapper;
import com.inool.model.Teacher;
import com.inool.service.TeacherService;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class TeacherServiceImpl extends ServiceImpl<TeacherMapper, Teacher> implements TeacherService {

    @Override
    public Teacher addTeacher(Teacher teacher) {
        save(teacher);
        return teacher;
    }

    @Override
    public Teacher updateTeacherInfo(String id, Teacher teacher) {
        teacher.setStaffNumber(id);
        updateById(teacher);
        return teacher;
    }

    @Override
    public void removeTeacher(String id) {
        removeById(id);
    }

    @Override
    public Teacher getTeacherById(String id) {
        return getById(id);
    }

    @Override
    public List<Teacher> getAllTeachers() {
        return list();
    }

    @Override
    public Page<Teacher> pageTeachers(long current, long size,
                                      String staffNumber, String name, String title) {
        Page<Teacher> page = new Page<>(current, size);
        QueryWrapper<Teacher> wrapper = new QueryWrapper<>();
        if (staffNumber != null && !staffNumber.equals("")) {
            wrapper.eq("staff_number", staffNumber);
        }
        if (name != null && !name.equals("")) {
            wrapper.like("name", name);
        }
        if (title != null && !title.equals("")) {
            wrapper.eq("title", title);
        }
        this.page(page, wrapper);
        return page;
    }
}    